﻿using Moq;
using MvvmHelpers;
using NUnit.Framework;
using System.Threading.Tasks;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using TabbedTemplate.ViewModels;

namespace UnitTest.ViewModels
{
    public class TestMyPunchesPageViewModel
    {
        [Test]
        public async Task TestPageAppearingCommandCollectionNotNull()
        {
            var punchinfoStorage = new PunchInfoStorage(new Mock<IPreferenceStorage>().Object);
            await punchinfoStorage.DeleteTableAsync();
            await punchinfoStorage.PunchInfoStorageInitializeAsync();
            var punchInfos
                = new ObservableRangeCollection<PunchTimeStampParameter>();
            for (int i = 1; i < 18; i++)
            {
                punchInfos.Add(new PunchTimeStampParameter());
            }
            ;
            var myPunchesPageViewModel = new MyPunchesPageViewModel(punchinfoStorage, null, null, null, null);
            myPunchesPageViewModel.PunchInfoCollection = punchInfos;
            myPunchesPageViewModel.PageAppearingCommandFunction();
            Assert.IsFalse("暂无任何打卡记录" == myPunchesPageViewModel.Status);

        }

        [Test]
        public async Task TestPageAppearingCommandCollectionIsNull()
        {
            var punchinfoStorage = new PunchInfoStorage(new Mock<IPreferenceStorage>().Object);
            await punchinfoStorage.DeleteTableAsync();
            await punchinfoStorage.PunchInfoStorageInitializeAsync();
            var punchInfos
                = new ObservableRangeCollection<PunchTimeStampParameter>();
            var myPunchesPageViewModel = new MyPunchesPageViewModel(punchinfoStorage, null, null, null, null);
            myPunchesPageViewModel.PunchInfoCollection = punchInfos;
            myPunchesPageViewModel.PageAppearingCommandFunction();
            Assert.IsTrue("暂无任何打卡记录" == myPunchesPageViewModel.Status);
        }

        //[Test]
        //public async Task Test()
        //{
        //    var punchinfoStorage1 = new PunchInfoStorage(new Mock<IPreferenceStorage>().Object);
        //    await punchinfoStorage1.PunchInfoStorageInitializeAsync();
        //    var punchinfoStorage = new Mock<IPunchInfoStorage>().Object;
        //    await punchinfoStorage.PunchInfoStorageInitializeAsync();
        //    var b = await punchinfoStorage1.GetAllPunchInfosAsync();
        //    var c = await punchinfoStorage.GetAllPunchInfosAsync();
        //}

        [Test]
        public async Task TestPunchInfoTappedCommand()
        {
            var punchNavigationServiceMock =
                new Mock<IPunchNavigationService>();
            var mockPunchNavigationService = punchNavigationServiceMock.Object;
            var storeStorageMock = new Mock<IStoreStorage>();
            var mockStorage = storeStorageMock.Object;
            var preferenceStorageMock = new Mock<IPreferenceStorage>();
            var mockPreferenceStorage = preferenceStorageMock.Object;

            var myPunchesPageViewModel = new MyPunchesPageViewModel(null, mockPreferenceStorage, mockStorage, null, mockPunchNavigationService);

            var storeToReturn = new Store();
            storeStorageMock.Setup(p => p.GetStoreByStoreUidAsync("123"))
                .ReturnsAsync(storeToReturn);
            var punchTimeStampParameter = new PunchTimeStampParameter
            {
                PunchInfo = new PunchInfo
                {
                    Store_Uid = "123"
                },
                Store = new Store()
            };

            await myPunchesPageViewModel.PunchInfoTappedCommandFunction(punchTimeStampParameter);
            storeStorageMock.Verify(p => p.GetStoreByStoreUidAsync(
                "123"), Times.Once);
            punchNavigationServiceMock.Verify(p => p.NavigateToPunchViewPage(punchTimeStampParameter), Times.Once);
        }

        /// <summary>
        /// 删除PunchInfo测试。
        /// </summary>
        /// <returns></returns>
        [Test]
        public async Task TestDeletePunchInfoCommandFunction()
        {
            var punchTimeStampParameter = new PunchTimeStampParameter
            {
                PunchInfo = new PunchInfo { Id = 1, Timestamp = 123 }
            };
            var punchInfoStorageMock = new Mock<IPunchInfoStorage>();
            var mockPunchInfoStorage = punchInfoStorageMock.Object;
            var imageServiceMock = new Mock<IImageService>();
            var mockImageService = imageServiceMock.Object;
            var myPunchesPageViewModel =
                new MyPunchesPageViewModel(mockPunchInfoStorage, null, null, mockImageService, null);
            var collection =
                new ObservableRangeCollection<PunchTimeStampParameter>();
            collection.Add(punchTimeStampParameter);
            myPunchesPageViewModel.PunchInfoCollection = collection;

            await myPunchesPageViewModel.DeletePunchInfoCommandFunction(
                punchTimeStampParameter);
            punchInfoStorageMock.Verify(p => p.DeletePunchInfoByIdAsync(punchTimeStampParameter.PunchInfo), Times.Once);
            imageServiceMock.Verify(p => p.DeleteFolder(punchTimeStampParameter.PunchInfo.Timestamp), Times.Once);

        }

        [Test]
        public void TestPageAppearingCommandFunctionCollectionIsNull()
        {
            var myPunchesPageViewModel =
                new MyPunchesPageViewModel(null, null, null, null, null);
            var collection =
                new ObservableRangeCollection<PunchTimeStampParameter>();

            myPunchesPageViewModel.PunchInfoCollection = collection;

            myPunchesPageViewModel.PageAppearingCommandFunction();
            Assert.IsTrue(myPunchesPageViewModel.Status == "暂无任何打卡记录");
        }

        [Test]
        public void TestPageAppearingCommandFunctionCollectionNotNull()
        {
            var myPunchesPageViewModel =
                new MyPunchesPageViewModel(null, null, null, null, null);
            var collection =
                new ObservableRangeCollection<PunchTimeStampParameter>();
            var punchTimeStampParameter = new PunchTimeStampParameter
            {
                PunchInfo = new PunchInfo { Id = 1, Timestamp = 123 }
            };
            collection.Add(punchTimeStampParameter);
            myPunchesPageViewModel.PunchInfoCollection = collection;

            myPunchesPageViewModel.PageAppearingCommandFunction();
            Assert.IsFalse(myPunchesPageViewModel.Status == "暂无任何打卡记录");
        }
    }
}
